﻿package view.components 
{
	import caurina.transitions.Tweener;
	import fl.data.DataProvider;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	/**
	 * ...
	 * @author Slavshik Alexander
	 */
	public class TabsMenu extends TabMenuTemplate
	{
		private var menu:Array;
		private var menuItems/*TabMenuItem*/:Array;
		public var selected:int = 0;
		
		public function TabsMenu(arr:Array) {
			menu_mc.selected_mc.stop();
			menu = arr;
			buildMenu();
		}
		private function buildMenu():void {
			var xs:Number = 0;
			menuItems = [];
			for (var i:int = 0; i < menu.length; i++) {
				var item:TabMenuItem = new TabMenuItem();
				item.text = menu[i];
				item.name = "i" + i;
				item.x = xs;
				menuItems.push(item);
				item.addEventListener(MouseEvent.CLICK, onItemClick);
				xs += item.width + 5;
				addChild(item);
			}
			unselected_mc.width = xs;
			selectItem(selected);
		}
		private function onItemClick(e:MouseEvent):void {
			selectItem(int(e.currentTarget.name.substr(1)));
		}
		public function selectItem(idx:int):void {
			menuItems[idx].selected = true;
			menu_mc.selected_mc.gotoAndStop(idx ? 1 : 2);
			var xs:Number = -10;
			menu_mc.selected2_mc.visible = true;
			menu_mc.selected_mc.visible = false;
			menu_mc.selected_mc.width = menuItems[idx].width + 10;
			menu_mc.selected2_mc.width = menuItems[idx].width + 5;
			if (idx) {
				menu_mc.selected2_mc.visible = false;
				menu_mc.selected_mc.visible = true;
				xs = menuItems[idx].x - 5;
			}
			menu_mc.selected_mc.x = xs;
			menu_mc.selected_mc.alpha = menu_mc.selected2_mc.alpha = 0;
			Tweener.addTween(menu_mc.selected_mc, { alpha:1, time:0.5 } );
			Tweener.addTween(menu_mc.selected2_mc, { alpha:1, time:0.5 } );
			selected = idx;
			dispatchEvent(new Event(Event.CHANGE));
		}
		
	}
	
}